package com.check.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.check.dao.AgencyDao;
import com.check.dao.PersonRoleDao;
import com.check.dao.WorkRoleDao;
import com.check.pojo.Opr;
import com.check.pojo.Org;
import com.check.pojo.PageObject;
import com.check.pojo.WorkRole;
import com.check.service.AgencyService;
import com.check.service.WorkRoleService;

@Service("agencyService")
public class AgencyServiceImpl  implements AgencyService{

	@Autowired
	@Qualifier("agencyDao")
	private AgencyDao agencyDao;
	
	@Autowired
	@Qualifier("personRoleDao")
	private PersonRoleDao personRoleDao;
	
	public List<Org> findAllOrg() {
		
		return agencyDao.findAllOrg(new HashMap());
	}
	
	public String getOrgTreeJson(){
		List<Org> list = agencyDao.findAllOrg(new HashMap());
		StringBuilder sb=new StringBuilder("[");
		for (int i = 0; i < list.size(); i++) {
				Org org=list.get(i);
				if(i==list.size()-1){
					sb.append("{");
					sb.append("id:'"+org.getO_id()+"',");
					sb.append("pId:'"+org.getO_upid()+"',");
					sb.append("name:'"+org.getO_name()+"',");
					sb.append("open:false");
					sb.append("}");
				}else{
					sb.append("{");
					sb.append("id:'"+org.getO_id()+"',");
					sb.append("pId:'"+org.getO_upid()+"',");
					sb.append("name:'"+org.getO_name()+"',");
					sb.append("open:false");
					sb.append("},");
				}
		}
		sb.append("]");
		
		return sb.toString();
		
	}

	public Org findOrgById(String o_id) {
		Org org=null;
		try {
			org=agencyDao.findOrgById(o_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return org;
	}
	
	public boolean insertOrg(Org org){
		boolean flag=false;
		try {
			agencyDao.insertOrg(org);
			if(org.getOprId()!=null){
				
				List<Opr> list=personRoleDao.findAllRoleByOrgId(org.getO_upid());
				for (Opr opr : list) {
					Opr newopr=new  Opr();
					newopr.setOprId(opr.getOprId());
					newopr.setOrgId(org.getO_id());
					personRoleDao.insertPersonOrg(newopr);
				}
			}
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}
	
	public boolean updatetOrg(Org org){
		boolean flag=false;
		try {
			agencyDao.updateOrg(org);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}
	
	public boolean deleteOrg(String o_id,Opr opr){
		boolean flag=false;
		try {
			agencyDao.deleteOprOfOrg(o_id);
			agencyDao.deleteOrg(o_id);
			if(opr!=null){
				opr.setOrgId(o_id);
				personRoleDao.deleteOprOrgByOprIdAndOrgId(opr);
			}
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	public int findOrgCount(){
		return agencyDao.findOrgCount();
	}
	
	public List<Org> findAllOrgByType(String type){
		return agencyDao.findAllOrgByType(type);
	}
	
	public List<Org> findAllOrgByUpId(String deptId){
		return agencyDao.findAllOrgRoomByUpId(deptId);
	}
	
	public List<Org> findAllFirstLevelDept(){
		return agencyDao.findAllFirstLevelDept();
	}
	
	public List<Org> findAllNextLevelDept(String o_upid){
		return agencyDao.findAllNextLevelDept(o_upid);
	}

	public String getOrgTreeJsonByOrg(String orgId,String flag) {
//		List<Org> list = agencyDao.findAllCurrentAndNextOrg(orgId);
		
		Map map=new HashMap();
		map.put("upid", orgId);
		map.put("flag", flag);
		List<Org> list = agencyDao.findAllOrgByUpId(map);
		StringBuilder sb=new StringBuilder("[");
		for (int i = 0; i < list.size(); i++) {
				Org org=list.get(i);
				if(!"2".equals(flag)){
					if(orgId.equals(org.getO_id())){
						continue;
					}
				}
				sb.append("{");
				sb.append("id:'"+org.getO_id()+"',");
				sb.append("pId:'"+org.getO_upid()+"',");
				sb.append("name:'"+org.getO_name()+"',");
				if(org.getO_orgcount()>0){
					sb.append("isParent:true,");
				}else{
					sb.append("isParent:false,");
				}
				sb.append("open:false");
				if(i==list.size()-1){
					sb.append("}");
				}else{
					sb.append("},");
				}
		}
		sb.append("]");
		
		return sb.toString();
		
	}

	public String getOrgCheckedTreeJson(String oprId) {
		List<Org> list=agencyDao.findAllOrgByOprId(oprId);
		StringBuilder sb=new StringBuilder("[");
		for (int i = 0; i < list.size(); i++) {
				Org org=list.get(i);
				sb.append("{");
				sb.append("id:'"+org.getO_id()+"',");
				sb.append("pId:'"+org.getO_upid()+"',");
				sb.append("name:'"+org.getO_name()+"',");
				if(org.getChecked()==1){
					sb.append("checked:true");
				}else{
					sb.append("checked:false");
				}
				if(i==list.size()-1){
					sb.append("}");
				}else{
					sb.append("},");
				}
		}
		sb.append("]");
		
		return sb.toString();
		
	}

	public String getOrgTreeJsonByOprId(String oprId, String orgId, String flag) {
		Org findOrg=new Org();
		findOrg.setO_upid(orgId);
		findOrg.setOprId(oprId);
		List<Org> list = agencyDao.findAllOrgTreeByOprId(findOrg);
		StringBuilder sb=new StringBuilder("[");
		for (int i = 0; i < list.size(); i++) {
				Org org=list.get(i);
				if(!"2".equals(flag)){
					if(orgId.equals(org.getO_id())){
						continue;
					}
				}
				sb.append("{");
				sb.append("id:'"+org.getO_id()+"',");
				sb.append("pId:'"+org.getO_upid()+"',");
				sb.append("name:'"+org.getO_name()+"',");
				if(org.getO_orgcount()>0){
					sb.append("isParent:true,");
				}else{
					sb.append("isParent:false,");
				}
				sb.append("open:false");
				if(i==list.size()-1){
					sb.append("}");
				}else{
					sb.append("},");
				}
		}
		sb.append("]");
		
		return sb.toString();
	}

	public String findFirstLevelUpOrgId(String oprId) {
		List<Org> list=agencyDao.findFirstLevelOrgId(oprId);
		String id="";
		if(list.size()>0){
			Org org = list.get(0);
			id=org.getO_upid();
		}
		return id;
	}

	public String findFirstLevelOrgId(String oprId) {
		List<Org> list=agencyDao.findFirstLevelOrgId(oprId);
		String id="";
		if(list.size()>0){
			Org org = list.get(0);
			id=org.getO_id();
		}
		return id;
	}
	
}
